package team.archai.nexus.boot.web.handlers;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;
import team.archai.nexus.boot.tool.HttpUtil;
import team.archai.nexus.boot.web.response.ResultUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户登出成功处理类
 * @author Mr.Longyx
 * @date 2020年01月11日 9:57
 */
@Component
@ConditionalOnProperty(name = "archai-nexus.auth.type", havingValue = "security")
public class LogoutHandler implements LogoutSuccessHandler {
    /**
     * 用户登出返回结果
     * 这里应该让前端清除掉Token
     * @Author Longyx
     * @CreateTime 2019/10/3 9:50
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication){
        SecurityContextHolder.clearContext();
        HttpUtil.responseJson(response, ResultUtil.success(authentication.getPrincipal()));
    }
}
